package programmteile;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class BarChart_mit_Zufallswerten extends JPanel {
	private double[] wert;

	  private String[] name;

	  private String titel;

	public BarChart_mit_Zufallswerten(double[] v, String[] n, String t) {
		setBorder(new TitledBorder(null, "Chart", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setToolTipText("Chart");
		setLayout(null);
	    name = n;
	    wert = v;
	    titel = t;
	  }
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    if (wert == null || wert.length == 0)
	      return;
	    double minValue = 0;
	    double maxValue = 0;
	    for (int i = 0; i < wert.length; i++) {
	      if (minValue > wert[i])
	        minValue = wert[i];
	      if (maxValue < wert[i])
	        maxValue = wert[i];
	    }

	    Dimension d = getSize();
	    int clientWidth = d.width;
	    int clientHeight = d.height;
	    int barWidth = clientWidth / wert.length;

	    Font titleFont = new Font("SansSerif", Font.BOLD, 20);
	    FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
	    Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
	    FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

	    int titleWidth = titleFontMetrics.stringWidth(titel);
	    int y = titleFontMetrics.getAscent();
	    int x = (clientWidth - titleWidth) / 2;
	    g.setFont(titleFont);
	    g.drawString(titel, x, y);

	    int top = titleFontMetrics.getHeight();
	    int bottom = labelFontMetrics.getHeight();
	    if (maxValue == minValue)
	      return;
	    double scale = (clientHeight - top - bottom) / (maxValue - minValue);
	    y = clientHeight - labelFontMetrics.getDescent();
	    g.setFont(labelFont);

	    for (int i = 0; i < wert.length; i++) {
	      int valueX = i * barWidth + 1;
	      int valueY = top;
	      int height = (int) (wert[i] * scale);
	      if (wert[i] >= 0)
	        valueY += (int) ((maxValue - wert[i]) * scale);
	      else {
	        valueY += (int) (maxValue * scale);
	        height = -height;
	      }

	      g.setColor(Color.blue);
	      g.fillRect(valueX, valueY, barWidth - 2, height);
	      g.setColor(Color.black);
	      g.drawRect(valueX, valueY, barWidth - 2, height);
	      int labelWidth = labelFontMetrics.stringWidth(name[i]);
	      x = i * barWidth + (barWidth - labelWidth) / 2;
	      g.drawString(name[i], x, y);
	    }
	  }

	  public static void main(String[] argv) {
	    JFrame f = new JFrame();
	    f.setSize(400, 300);
	    double[] values = new double[3];
	    String[] names = new String[3];
	    values[0] = Math.random();
	    names[0] = "Wert 1";
	    System.out.println(values[0]);

	    values[1] = Math.random();
	    names[1] = "Wert 2";
	    System.out.println(values[1]);

	    values[2] = Math.random();
	    names[2] = "Wert 3";
	    System.out.println(values[2]);

	    f.getContentPane().add(new BarChart_mit_Zufallswerten(values, names, "Bar Chart"));

	    WindowListener wndCloser = new WindowAdapter() {
	      public void windowClosing(WindowEvent e) {
	        System.exit(0);
	      }
	    };
	    f.addWindowListener(wndCloser);
	    f.setVisible(true);
	  }
	}
